#include <iostream>
#include "hs_rasterdata.h"

template<typename DataType>
void printmat(const hs::raster::RasterView<DataType>& mat)
{
    for (int y = 0; y < mat.sizeY(); ++y) {
        for (int x = 0; x < mat.sizeX(); ++x) {
            // std::cout << mat(x, y) << ", ";
            printf("%6.0f", mat(x, y));
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

int main(int argc, char* argv[])
{
    int size_x = 5;
    int size_y = 4;
    float* pdata = new float[size_x * size_y];
    for (int i = 0; i < size_x * size_y; ++i) {
        pdata[i] = (float)(i + 1);
    }

    /* 行优先存储RasterData */
    hs::raster::RasterData<float> rdata(size_x, size_y, true);
    for (int i = 0; i < size_x * size_y; ++i) {
        rdata.view().pData()[i] = pdata[i];
    }
    std::cout << "Size X: " << rdata.view().sizeX() 
              << ", Size Y: " << rdata.view().sizeY()
              << ", Is Row Major: " << rdata.view().isRowMajor() << std::endl;
    std::cout << "rdata: \n";
    printmat(rdata.view());

    /* 列优先存储RasterData */
    hs::raster::RasterData<float> cdata(size_x, size_y, false);
    for (int i = 0; i < size_x * size_y; ++i) {
        cdata.view().pData()[i] = pdata[i];
    }
    std::cout << "Size X: " << cdata.view().sizeX() 
              << ", Size Y: " << cdata.view().sizeY()
              << ", Is Row Major: " << cdata.view().isRowMajor() << std::endl;
    std::cout << "rdata: \n";
    printmat(cdata.view());

    /* 拷贝构造函数(交出所有权) */
    std::cout << "Testing [ hs::raster::RasterData<float> rdata2(rdata) ]:" << std::endl;
    std::cout << " Before: rdata.empty: " << rdata.view().isEmpty() << std::endl;
    hs::raster::RasterData<float> rdata2(rdata);
    std::cout << " After : rdata.empty: " << rdata.view().isEmpty() << std::endl;
    std::cout << std::endl;

    /* 赋值构造函数(交出所有权) */
    std::cout << "Testing [ rdata3 = rdata2 ]:" << std::endl;
    std::cout << " Before: rdata2.empty: " << rdata2.view().isEmpty() << std::endl;
    hs::raster::RasterData<float> rdata3;
    rdata3 = rdata2;
    std::cout << " After : rdata2.empty: " << rdata2.view().isEmpty() << std::endl;
    std::cout << std::endl;

    /* 赋值移动构造函数(交出所有权) */
    std::cout << "Testing [ rdata3 = rdata2 ]:" << std::endl;
    std::cout << " Before: rdata3.empty: " << rdata3.view().isEmpty() << std::endl;
    hs::raster::RasterData<float> rdata4;
    rdata4 = std::move(rdata3);
    std::cout << " After : rdata3.empty: " << rdata3.view().isEmpty() << std::endl;
    std::cout << std::endl;

    /* 深拷贝(重新分配内存并进行数据拷贝) */
    std::cout << "Testing [ rdata5.copyFrom(rdata4) ]:" << std::endl;
    std::cout << " Before: rdata4.empty: " << rdata4.view().isEmpty() << std::endl;
    hs::raster::RasterData<float> rdata5;
    rdata5.copyFrom(rdata4);
    std::cout << " After : rdata4.empty: " << rdata4.view().isEmpty() << std::endl;
    std::cout << std::endl;

    /* 通过 RasterView 来获取数据的基本属性 */
    std::cout << "rdata5 information:\n";
    std::cout << " - Size X: " << rdata5.view().sizeX() << std::endl;
    std::cout << " - Size Y: " << rdata5.view().sizeY() << std::endl;
    std::cout << " - Nodata: " << rdata5.view().nodata() << std::endl;

    return 0;
}

